var req;
function clear(){
	validate("cleardiv","userIdMessage");
	//validate("cleardiv","passwordMessage");
}

function validate(action, target) {
	if (action == "validate_userid"){
	   var idField = document.getElementById("userid");
	   // call ValidateServlet
	   var url = "validate?action="+action+"&id=" + encodeURIComponent(idField.value);
	}
	if (action == "validate_password"){
	   var pwdField = document.getElementById("password");
	   // call ValidateServlet
	   var url = "validate?action="+action+"&password=" + encodeURIComponent(pwdField.value);
	}	
	if (action == "cleardiv"){
	   
	   // call ValidateServlet
	   var url = "validate?action="+action+"&targetdiv="+target;
	}	
	if (action == "validate_credentials"){
	   var idField = document.getElementById("userid");
	   var pwdField = document.getElementById("password");
	   // call ValidateServlet
	   var url = "validate?action="+action+"&id=" + encodeURIComponent(idField.value) + "&password=" + encodeURIComponent(pwdField.value);
	   
	   //var url = "http://localhost:8080/iplecture03/initialize1";
	}
	
	if (typeof XMLHttpRequest != "undefined") {
	    req = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
	    req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	req.open("GET", url, true);
	req.onreadystatechange = callback;
	req.send(null);
}
 
function callback() {
    if (req.readyState == 4) {
        if (req.status == 200) {
            // update the HTML DOM based on whether or not message is valid
            parseMessage();
            
        }
    }
}

function parseMessage() {
 	// get message from xml
 	var status = req.responseXML.getElementsByTagName("status");
 	var targetdiv = req.responseXML.getElementsByTagName("targetdiv");
 	var message = req.responseXML.getElementsByTagName("message");
 	setMessage(status[0].childNodes[0].nodeValue, 
 		targetdiv[0].childNodes[0].nodeValue,
 		message[0].childNodes[0].nodeValue);
 	//setDivHtml("Content",this.xmlHttpReq.responseText);
}

function setMessage(status, targetdiv, message) {
    var mdiv = document.getElementById(targetdiv);
    if (message == null){
    	message = "";
    }
    if (status == "invalid") {
		mdiv.innerHTML = "<div style=\"color:red\">"+message+"</ div>";
    } else if (status == "valid") {
		mdiv.innerHTML = "<div style=\"color:green\">"+message+"</ div>";
    } else if (status == "clear") {
    	mdiv.innerHTML = "<div> </ div>";
    } else if (status == "html") {
    	mdiv.innerHTML = message;
    }
}

function setDivHtml(targetdiv, html) {
	var mdiv = document.getElementById(targetdiv);
	mdiv.innerHTML = html;
}